const fs = require("fs");
const path = require("path");
const log_path = path.resolve(__dirname, "../../static/public/logger.txt");
async function Logger(ctx, next) {
  const one_log = `------------ ${new Date().toLocaleString()}------------
访问者ip: ${getClientIp(ctx.req)}
path: ${ctx.path} - ${ctx.method}, - body:${JSON.stringify(ctx.request.body)} - ,query ${JSON.stringify(ctx.query)}
`;
  fs.writeFile(
    log_path,
    one_log,
    {
      flag: "a+"
    },
    (err, data) => {
      if (err) console.log(err);
      console.log(one_log);
    }
  );
  await next();
}

module.exports = Logger;
// 获取访问者ip地址
function getClientIp(req) {
  return req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
}
